package com.tianxiao.faas.application.web.corner;

import com.tianxiao.faas.api.param.corner.BasePageParam;
import com.tianxiao.faas.api.param.corner.BazaarApplyParam;
import com.tianxiao.faas.api.param.corner.BazaarResult;
import com.tianxiao.faas.application.aop.AuthenticationContextHolder;
import com.tianxiao.faas.biz.service.corner.ActivityService;
import com.tianxiao.faas.biz.service.corner.ShopService;
import com.tianxiao.faas.common.access.Access;
import com.tianxiao.faas.common.result.Result;
import com.tianxiao.faas.mapper.dao.ActivityApplyModelMapper;
import com.tianxiao.faas.mapper.model.ShopModel;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/app/bazaar/")
public class BazaarController {
    @Resource
    private ActivityService activityService;

    @RequestMapping(value = "/index/images")
    public Result<Object> indexImage() {
        Map<String, String> map = new HashMap<>();
        map.put("bazaarImage", "https://cdn.cornsee.com/static/8addc85f-ed29-4f1c-a98c-b37afbe9536d.png");
        map.put("shopImage", "https://cdn.cornsee.com/static/1263daf6-c3c0-474a-8c4d-762053d2f070.png");
        return Result.builder().success(true).data(map);
    }

    @RequestMapping(value = "/list")
    @Access(login = false)
    public Result<List<BazaarResult>> list(@RequestBody BasePageParam param) {
        long now = System.currentTimeMillis();

        List<BazaarResult> results = activityService.list(param, now);
        Integer count = activityService.count(param, now);
        Long userId = AuthenticationContextHolder.currentContext().getUserId();
        activityService.checkJoined(results, userId);

        return Result.builder().success(true).data(results)
                .totalCount(count).pageNo(param.getPage()).pageSize(param.getPageSize()).totalPage();
    }

    //排序算法

    @RequestMapping(value = "/detail/get")
    @Access(login = false)
    public Result<List<BazaarResult>> get(Long id) {
        Long userId = AuthenticationContextHolder.currentContext().getUserId();
        BazaarResult bazaarResult = activityService.get(id, userId);
        return Result.builder().success(true).data(bazaarResult);
    }

    @RequestMapping(value = "/apply")
    @Access
    public Result<List<Boolean>> apply(@RequestBody BazaarApplyParam bazaarApplyParam) {
        Long userId = AuthenticationContextHolder.currentContext().getUserId();
        activityService.apply(bazaarApplyParam, userId);
        return Result.builder().success(true).data(true);
    }
}
